﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KaoQin.BLL;
using KaoQin.Model;
using KaoQin.Common;
using System.Data;
using System.IO;

namespace KaoQin.WPFApp.kaoQin
{
    /// <summary>
    /// PQuery.xaml 的交互逻辑
    /// </summary>
    public partial class PQuery : Window
    {
        public PQuery()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.initData();
            this.initDate();
        }

        private void initDate()
        {
            txtStartTime.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1";
            txtEndTime.Text = Convert.ToDateTime(txtStartTime.Text).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            txtStartTime2.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1";
            txtEndTime2.Text = Convert.ToDateTime(txtStartTime.Text).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            txtStartTime3.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1";
            txtEndTime3.Text = Convert.ToDateTime(txtStartTime.Text).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            txtStartTime4.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1";
            txtEndTime4.Text = Convert.ToDateTime(txtStartTime.Text).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            cbswipeState.ItemsSource = StaticDataHelper.GetSwipeState();
        }

        private void initData()
        {
            try
            {
                this.bindList();

            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定数据出错：" + ex.Message);
            }
        }

        private void bindList()
        {
            lstEmployee.ItemsSource = new Employee().GetModelList("KName like '%" + txtKey.Text.Trim() + "%'");
        }
        

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.bindList();
        }

        private void lstEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lsvInfo.ItemsSource = null;
        }



        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            bindSwipeList();
        }

        private void bindSwipeList()
        {
            try
            {
                EmployeeInfo empInfo = (EmployeeInfo)gridInfo.DataContext;

                if (empInfo == null)
                {
                    MessageBox.Show("请选择一个员工！");
                    return;
                }
                string strWhere = " where";

                strWhere += "  EmployeeID='" + empInfo.ID + "' ";
                if (!string.IsNullOrEmpty(txtStartTime.Text))
                {
                    strWhere += " and SwipeCardTime>=datetime('" + Convert.ToDateTime(txtStartTime.Text).ToString("yyyy-MM-dd") + " 00:00:00')";
                }

                if (!string.IsNullOrEmpty(txtEndTime.Text))
                {
                    strWhere += " and SwipeCardTime<=datetime('" + Convert.ToDateTime(txtEndTime.Text).ToString("yyyy-MM-dd") + " 23:59:59')";
                }

                if (cbswipeState.SelectedValue.ToString()!="-1")
                {
                    strWhere += " and KState=" + cbswipeState.SelectedValue.ToString();
                }



                int recordCount;
                pager.PageSize = 10;

                lsvInfo.ItemsSource = new SwipeCardRecord().GetListByPage(strWhere, "SwipeCardTime desc ", pager.PageIndex, pager.PageSize, out recordCount).Tables[0].DefaultView;
                pager.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询出错：" + ex.Message);
            }
        }

        private void bindLeaveList()
        {
            try
            {
                EmployeeInfo empInfo = (EmployeeInfo)gridInfo.DataContext;

                if (empInfo == null)
                {
                    MessageBox.Show("请选择一个员工！");
                    return;
                }
                string strWhere = " where";

                strWhere += "  EmployeeID='" + empInfo.ID + "' ";
                if (!string.IsNullOrEmpty(txtStartTime2.Text))
                {
                    strWhere += " and CreateTime>=datetime('" + Convert.ToDateTime(txtStartTime2.Text).ToString("yyyy-MM-dd") + " 00:00:00')";
                }

                if (!string.IsNullOrEmpty(txtEndTime2.Text))
                {
                    strWhere += " and CreateTime<=datetime('" + Convert.ToDateTime(txtEndTime2.Text).ToString("yyyy-MM-dd") + " 23:59:59')";
                }




                int recordCount;
                pager2.PageSize = 10;

                lsvInfo2.ItemsSource = new BLL.LeaveMgr().GetListByPage(strWhere, "CreateTime desc ", pager2.PageIndex, pager2.PageSize, out recordCount).Tables[0].DefaultView;
                pager2.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询出错：" + ex.Message);
            }
        }
        private void bindOverTimeList()
        {
            try
            {
                EmployeeInfo empInfo = (EmployeeInfo)gridInfo.DataContext;

                if (empInfo == null)
                {
                    MessageBox.Show("请选择一个员工！");
                    return;
                }
                string strWhere = " where";

                strWhere += "  EmployeeID='" + empInfo.ID + "' ";
                if (!string.IsNullOrEmpty(txtStartTime3.Text))
                {
                    strWhere += " and CreateTime>=datetime('" + Convert.ToDateTime(txtStartTime3.Text).ToString("yyyy-MM-dd") + " 00:00:00')";
                }

                if (!string.IsNullOrEmpty(txtEndTime3.Text))
                {
                    strWhere += " and CreateTime<=datetime('" + Convert.ToDateTime(txtEndTime3.Text).ToString("yyyy-MM-dd") + " 23:59:59')";
                }




                int recordCount;
                pager3.PageSize = 10;

                lsvInfo3.ItemsSource = new BLL.OverTimeMgr().GetListByPage(strWhere, "CreateTime desc ", pager3.PageIndex, pager3.PageSize, out recordCount).Tables[0].DefaultView;
                pager3.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询出错：" + ex.Message);
            }
        }

        private void bindGoOutList()
        {
            try
            {
                EmployeeInfo empInfo = (EmployeeInfo)gridInfo.DataContext;

                if (empInfo == null)
                {
                    MessageBox.Show("请选择一个员工！");
                    return;
                }
                string strWhere = " where";

                strWhere += "  EmployeeID='" + empInfo.ID + "' ";
                if (!string.IsNullOrEmpty(txtStartTime4.Text))
                {
                    strWhere += " and CreateTime>=datetime('" + Convert.ToDateTime(txtStartTime4.Text).ToString("yyyy-MM-dd") + " 00:00:00')";
                }

                if (!string.IsNullOrEmpty(txtEndTime4.Text))
                {
                    strWhere += " and CreateTime<=datetime('" + Convert.ToDateTime(txtEndTime4.Text).ToString("yyyy-MM-dd") + " 23:59:59')";
                }




                int recordCount;
                pager4.PageSize = 10;

                lsvInfo4.ItemsSource = new BLL.GoOutMgr().GetListByPage(strWhere, "CreateTime desc ", pager4.PageIndex, pager4.PageSize, out recordCount).Tables[0].DefaultView;
                pager4.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询出错：" + ex.Message);
            }
        }

        private void btnLook_Click(object sender, RoutedEventArgs e)
        {
            string photoPath = (sender as Button).Tag.ToString();
            if (File.Exists(FileHelper.GetcurrPath() + "\\" + photoPath))
            {
                BigPhoto bigphotodlg = new BigPhoto();
                bigphotodlg.SetImg(photoPath);
                bigphotodlg.ShowDialog();
            }
            else
            {
                MessageBox.Show("照片不存在");

            }
            
            
        }

        private void DataPager_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            bindSwipeList();
        }

        private void btnOK2_Click(object sender, RoutedEventArgs e)
        {
            this.bindLeaveList();
        }

        private void pager2_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            this.bindLeaveList();
        }

        private void btnOK3_Click(object sender, RoutedEventArgs e)
        {
            this.bindOverTimeList();
        }

        private void pager3_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            this.bindOverTimeList();
        }

        private void btnOK4_Click(object sender, RoutedEventArgs e)
        {
            this.bindGoOutList();
        }

        private void pager4_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            this.bindGoOutList();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            string ID = (sender as Button).Tag.ToString();
            try
            {
                EditSwipe editswipe = new EditSwipe(ID);
                editswipe.ShowDialog();
                if (editswipe.DialogResult == true)
                {
                    bindSwipeList();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改打卡时间出错：" + ex.Message);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EmployeeInfo empInfo = (EmployeeInfo)gridInfo.DataContext;

                if (empInfo == null)
                {
                    MessageBox.Show("请选择一个员工！");
                    return;
                }
                string strWhere = " where";

                strWhere += "  EmployeeID='" + empInfo.ID + "' ";
                if (!string.IsNullOrEmpty(txtStartTime.Text))
                {
                    strWhere += " and SwipeCardTime>=datetime('" + Convert.ToDateTime(txtStartTime.Text).ToString("yyyy-MM-dd") + " 00:00:00')";
                }

                if (!string.IsNullOrEmpty(txtEndTime.Text))
                {
                    strWhere += " and SwipeCardTime<=datetime('" + Convert.ToDateTime(txtEndTime.Text).ToString("yyyy-MM-dd") + " 23:59:59')";
                }

                if (cbswipeState.SelectedValue.ToString() != "-1")
                {
                    strWhere += " and KState=" + cbswipeState.SelectedValue.ToString();
                }
                btnExport.IsEnabled = false;
                DataSet ds= new SpecialCmd().ExportPData(strWhere);
                if (ds.Tables[0]==null||ds.Tables[0].Rows.Count==0)
                {
                    MessageBox.Show("没有数据");
                    btnExport.IsEnabled = true;
                    return;
                }
                
                ExportFile.SaveAsExcelSetFile(ds);

                btnExport.IsEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show("导出出错：" + ex.Message);
                btnExport.IsEnabled = true;
                return;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("确定删除吗？", "确定", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    string id = (sender as Button).Tag.ToString();
                    new BLL.SwipeCardRecord().Delete(id);
                    MessageBox.Show("删除成功");

                    bindSwipeList();
                }



            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败:" + ex.Message);
            }
        }
    }
}
